package com.study.webapp.lock;

import java.util.concurrent.locks.ReentrantLock;

/**
 * 公平锁与非公平锁
 * 
 * @author admin
 *
 *         2018年12月19日
 */
public class failrLock {
	// 填true表示是公平锁，false非公平，默认false可不填，
	private static ReentrantLock lock = new ReentrantLock(true);

	private static Runnable runnable = () -> {
		for (int i = 0; i < 10000; i++) {
			try {
				lock.lock();
				System.out.println(Thread.currentThread().getName());
			} finally {
				lock.unlock();
			}
		}
	};

	public static void main(String[] args) {
		Thread t1 = new Thread(runnable, "t--1");
		Thread t2 = new Thread(runnable, "t--2");
		Thread t3 = new Thread(runnable, "t--3");
		t1.start();
		t2.start();
		t3.start();
	}

}
